using System;
using System.Collections.Generic;
using System.Text;
using RapidHDL;


namespace SimpleCounter
{
    class ToplevelHardware : PadFrame 
    {
        public override bool ConfigureSettings()
        {
            Settings.UseStart = false;
            Settings.BuildHardware = true;
            Settings.RunFromXilinx = true;
            Settings.ClockFrequency = 1;
            Settings.UseSDRAM = false;
            Settings.UseReset = true;
            Settings.SynthASIC = false;
            Settings.SimulateVerilog = true;
            Settings.ForceBuildHardware = true;
            Settings.BuildRemote = false;
            
            //Settings.BuildServerPath = "buildserver:8080";
            //Settings.RemoteFPGAServer = "fpgaserver:3406";

            //Settings.BuildServerPath = "pc:3030";
            Settings.RemoteFPGAServer = "pc:3030";
            //Settings.RemoteFPGAServer = "141.210.135.71:3030";
            Settings.XilinxToolsPath = "D:\\Xilinx\\11.1\\ISE\\bin\\nt64\\";
            
            return true;
        }

        public override bool GenerateStructure(TopLevelComponent TopLevelComponent)
        {
            // create an instance of the upcounter object
            // Parameter 1 -> This is a child of the TopLevelComponent, passed into this function
            // Parameter 2 -> name this component as "counter"
            // Parameter 3 -> connect the Main Clock from the toplevel component
            // Parameter 4 -> count to 14
            // Parameter 5 -> count on the rising edge of the clock
            // Parameter 6 -> create an enable signal to control this counter
            // Parameter 7 -> reset this counter to 0
            // Parameter 8 -> count up by 1
            UpCounter oUpCounter = new UpCounter(TopLevelComponent, "counter", 
                TopLevelComponent.MainClock, 5, true, true, 1, 27, 1);
            

            // create a signal named "I_ENABLE" that we can control from software
            // it is connected to the "EnableIn" field of the up counter
            // it is 1 bit wide
            SystemInterface.AddSignalSource("I_ENABLE", oUpCounter.EnableIn, "1");

            // connect the upcounter's reset connection to the MainReset signal of our top component
            oUpCounter.ResetIn.Connection = TopLevelComponent.MainReset;

            AssignPinCLK(39);
            AssignPinGND(40);
            AssignPinVDD(20);
            AssignPinInput("I_ENABLE", 37);
            //AssignPinInput("I_RESET", 38);

            return true;
        }

        public override bool ConfigureSignalViews(TopLevelComponent TopLevelComponent)
        {
            // read the output of the counter in our software
            SystemInterface.AddSignalView("O_COUNT", "counter.COUNT_O");
            // specify the format of the output signal that we want to view
            SystemInterface.FormatSignalView("O_COUNT", SignalFormat.Unsigned);
            AssignPinOutput("O_COUNT", 31);
            
            return true;
        }

        public override bool GenerateHardwareScripts()
        {
            HardwareScript oScript = HardwareScripts.NewScript("test");            
            //oScript.SignalCheck(4,"O_COUNT","00001");
            oScript.SignalView(0, "O_COUNT");
            oScript.SignalCheck(0, "O_COUNT", "1");
            oScript.SignalView(1, "O_COUNT");
            oScript.SignalCheck(1, "O_COUNT", "2");
            oScript.SignalView(2, "O_COUNT");
            oScript.SignalCheck(2, "O_COUNT", "3");
            oScript.SignalView(3, "O_COUNT");
            oScript.SignalCheck(3, "O_COUNT", "4");
            oScript.SignalView(4, "O_COUNT");
            oScript.SignalCheck(4, "O_COUNT", "5");
            oScript.SignalView(5, "O_COUNT");
            oScript.SignalCheck(5, "O_COUNT", "6");
            oScript.SignalOut(6, "I_ENABLE", "0");
            //oScript.SignalCheck(6, "O_COUNT", "6");
            oScript.SignalView(7, "O_COUNT");
            //oScript.SignalCheck(7, "O_COUNT", "6");
            //oScript.SignalCheck(8, "O_COUNT", "6");
            //oScript.SignalCheck(9, "O_COUNT", "6");
            oScript.SignalOut(10, "I_ENABLE", "1");
            //oScript.SignalCheck(10, "O_COUNT", "7");
            //oScript.SignalCheck(11, "O_COUNT", "8");
            //oScript.SignalCheck(12, "O_COUNT", "9");
            //oScript.SignalCheck(13, "O_COUNT", "10");
            //oScript.SignalCheck(14, "O_COUNT", "11");
            //oScript.SignalCheck(15, "O_COUNT", "12");
            //oScript.SignalCheck(16, "O_COUNT", "13");
            oScript.SignalView(30, "O_COUNT");
            //oScript.SignalCheck(30, "O_COUNT", "27");
            oScript.SignalView(35, "O_COUNT");
            //oScript.SignalCheck(35, "O_COUNT", "5");
            //oScript.DoScript(true);
            return true;
        }


    }
}
